/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bbmangadownloader.gui;

import bbmangadownloader.manager.ConfigManager;
import bbmangadownloader.manager.FileManager;
import bbmangadownloader.manager.HttpDownloadManager;
import bbmangadownloader.ult.GUIUtilities;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Bach
 */
public class ConfigurationDialog extends javax.swing.JDialog {

    /**
     * Creates new form ConfigurationDialog
     */
    public ConfigurationDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        setTitle("Configuration");
        initComponents();
        initData();
    }

    private void initData() {
        ConfigManager config = ConfigManager.getCurrentInstance();
        synchronized (config) {
            // Maxium Number of Download
            {
                spnQueueMaxium.setValue(config.getMaxiumDownloadInQueue());
                spnQueueImageMaxium.setValue(config.getMaxiumDownloadImage());
            }

            cbxIsHttpd.setSelected(config.isHttpdServer());
            spnHttpdPort.setValue(config.getHttpdServerPort());
            spnHttpdPort.setEnabled(config.isHttpdServer());

            // Proxy
            boolean isUseProxy = config.getIsUsingProxy();
            cbxProxy.setSelected(isUseProxy);
            if (!isUseProxy) {
                txtProxyAddress.setEnabled(false);
                spnProxyPort.setEnabled(false);
            }
            {
                txtProxyAddress.setText(config.getProxyAddress());
                spnProxyPort.setValue(config.getProxyPort());
            }
            // Load Output
            {
                txtSaveTo.setSelectedFile(new File(config.getOutputFolder()));
                chbHtml.setSelected(config.isGenerateHtml());
                chbHtmlManifest.setEnabled(config.isGenerateHtml());
                chbHtmlManifest.setSelected(config.isGenerateHtmlManifest());
                chbZip.setSelected(config.isZip());
                if (chbZip.isSelected()) {
                    chbDeleteZip.setEnabled(true);
                }
                chbDeleteZip.setSelected(config.isDeleteAfterZip());
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel4 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        spnQueueMaxium = new javax.swing.JSpinner();
        jLabel7 = new javax.swing.JLabel();
        spnHttpdPort = new javax.swing.JSpinner();
        cbxIsHttpd = new javax.swing.JCheckBox();
        jLabel8 = new javax.swing.JLabel();
        spnQueueImageMaxium = new javax.swing.JSpinner();
        jLabel9 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        txtSaveTo = new bbmangadownloader.gui.control.JFolderChooser();
        chbZip = new javax.swing.JCheckBox();
        chbDeleteZip = new javax.swing.JCheckBox();
        chbHtml = new javax.swing.JCheckBox();
        chbHtmlManifest = new javax.swing.JCheckBox();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cbxProxy = new javax.swing.JCheckBox();
        jLabel2 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtProxyAddress = new javax.swing.JTextField();
        spnProxyPort = new javax.swing.JSpinner();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jButton1.setText("Save");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel1.add(jButton1);

        jLabel6.setText("Queue Size");

        spnQueueMaxium.setModel(new javax.swing.SpinnerNumberModel(1, 1, 100, 1));

        jLabel7.setText("Port (*)");

        spnHttpdPort.setModel(new javax.swing.SpinnerNumberModel(1, 1, 100, 1));
        spnHttpdPort.setEnabled(false);

        cbxIsHttpd.setText("File browser server (*)");
        cbxIsHttpd.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        cbxIsHttpd.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbxIsHttpdItemStateChanged(evt);
            }
        });

        jLabel8.setText("(*): Restart application to apply");

        spnQueueImageMaxium.setModel(new javax.swing.SpinnerNumberModel(1, 1, 100, 1));

        jLabel9.setText("Image Queue Size");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addGap(18, 18, 18)
                        .addComponent(spnQueueMaxium, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
                        .addComponent(jLabel9)
                        .addGap(18, 18, 18)
                        .addComponent(spnQueueImageMaxium, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel4Layout.createSequentialGroup()
                                .addComponent(cbxIsHttpd)
                                .addGap(62, 62, 62)
                                .addComponent(jLabel7)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(spnHttpdPort, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel8))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(spnQueueMaxium, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel9)
                        .addComponent(spnQueueImageMaxium, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(spnHttpdPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbxIsHttpd))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8)
                .addContainerGap())
        );

        jTabbedPane1.addTab("General", jPanel4);

        jLabel5.setText("Save to");

        chbZip.setText("Zip File");
        chbZip.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        chbZip.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chbZipActionPerformed(evt);
            }
        });

        chbDeleteZip.setText("Delete Image File After Zip");
        chbDeleteZip.setEnabled(false);
        chbDeleteZip.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        chbHtml.setText("Generate Html (Read in Browser)");
        chbHtml.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        chbHtml.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                chbHtmlItemStateChanged(evt);
            }
        });

        chbHtmlManifest.setText("Generate Offline Image Cache (Browser Cache)");
        chbHtmlManifest.setEnabled(false);
        chbHtmlManifest.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGap(4, 4, 4)
                .addComponent(jLabel5)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtSaveTo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(chbHtml)
                        .addGap(18, 18, 18)
                        .addComponent(chbHtmlManifest))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(chbZip, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(52, 52, 52)
                        .addComponent(chbDeleteZip)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtSaveTo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chbZip)
                    .addComponent(chbDeleteZip))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(chbHtml)
                    .addComponent(chbHtmlManifest))
                .addContainerGap(53, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Output", jPanel3);

        jLabel1.setText("Proxy");

        cbxProxy.setText("Use Proxy");
        cbxProxy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxProxyActionPerformed(evt);
            }
        });

        jLabel2.setText("HTTP Proxy");

        jLabel4.setText("Port");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(cbxProxy)
                        .addGap(0, 273, Short.MAX_VALUE))
                    .addComponent(txtProxyAddress, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(spnProxyPort, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cbxProxy))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtProxyAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(spnProxyPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(83, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("Connection", jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 527, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbxProxyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxProxyActionPerformed
        boolean isUseProxy = cbxProxy.isSelected();
        txtProxyAddress.setEnabled(isUseProxy);
        spnProxyPort.setEnabled(isUseProxy);
    }//GEN-LAST:event_cbxProxyActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        ConfigManager config = ConfigManager.getCurrentInstance();
        synchronized (config) {
            // Maxium
            config.setMaxiumDownloadInQueue((Integer) spnQueueMaxium.getValue());
            config.setMaxiumDownloadImage((Integer) spnQueueImageMaxium.getValue());
            // Httpd port
            config.setHttpdServer(cbxIsHttpd.isSelected());
            config.setHttpdServerPort(((Number) spnHttpdPort.getValue()).intValue());

            // Proxy
            HttpDownloadManager.DownloadConfig dConfig = HttpDownloadManager.config;
            synchronized (dConfig) {
                boolean isUseProxy = cbxProxy.isSelected();
                dConfig.setIsUsingProxy(isUseProxy);
                if (isUseProxy) {
                    String address = txtProxyAddress.getText();
                    int port = (Integer) spnProxyPort.getValue();
                    dConfig.setProxy(address, port);
                    config.setProxyAddress(address);
                    config.setProxyPort(port);
                }
                config.setIsUsingProxy(isUseProxy);
            }
            // Download                        

            File f = txtSaveTo.getSelectedFolder();
            try {
                f.mkdirs();
                if (f.isDirectory()) {
                    FileManager.setDownloadFolder(f);
                    config.setOutputFolder(f.getAbsolutePath());
                }
            } catch (Exception ex) {
            }

            // Zip, Html
            config.setZip(chbZip.isSelected());
            config.setDeleteAfterZip(chbDeleteZip.isSelected());
            config.setGenerateHtml(chbHtml.isSelected());
            config.setGenerateHtmlManifest(chbHtmlManifest.isSelected());
//            System.out.println("Save To: " + downloadFolder);
            try {
                // Save Config
                config.save();
                GUIUtilities.showDialog(this, "Saved configuration!");
                this.setVisible(false);
                this.dispose();
            } catch (Exception ex) {
                Logger.getLogger(ConfigurationDialog.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void chbZipActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_chbZipActionPerformed
        chbDeleteZip.setEnabled(chbZip.isSelected());
    }//GEN-LAST:event_chbZipActionPerformed

    private void chbHtmlItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_chbHtmlItemStateChanged
        chbHtmlManifest.setEnabled(chbHtml.isSelected());
    }//GEN-LAST:event_chbHtmlItemStateChanged

    private void cbxIsHttpdItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbxIsHttpdItemStateChanged
        spnHttpdPort.setEnabled(cbxIsHttpd.isSelected());
    }//GEN-LAST:event_cbxIsHttpdItemStateChanged
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox cbxIsHttpd;
    private javax.swing.JCheckBox cbxProxy;
    private javax.swing.JCheckBox chbDeleteZip;
    private javax.swing.JCheckBox chbHtml;
    private javax.swing.JCheckBox chbHtmlManifest;
    private javax.swing.JCheckBox chbZip;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JSpinner spnHttpdPort;
    private javax.swing.JSpinner spnProxyPort;
    private javax.swing.JSpinner spnQueueImageMaxium;
    private javax.swing.JSpinner spnQueueMaxium;
    private javax.swing.JTextField txtProxyAddress;
    private bbmangadownloader.gui.control.JFolderChooser txtSaveTo;
    // End of variables declaration//GEN-END:variables
}
